package org.jeecg.modules.solitaire_app.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.YsyMeal;
import org.jeecg.modules.solitaire_app.app.entity.YsyMealOrder;
import org.jeecg.modules.solitaire_app.app.mapper.YsyMealMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyMealOrderMapper;
import org.jeecg.modules.solitaire_app.app.query.AddMetalOrderQuery;
import org.jeecg.modules.solitaire_app.app.service.IYsyMealOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 套餐订单表
 * @Author: jeecg-boot
 * @Date: 2023-08-29
 * @Version: V1.0
 */
@Service
@Slf4j
@DS("ysy_db")
public class YsyMealOrderServiceImpl extends ServiceImpl<YsyMealOrderMapper, YsyMealOrder> implements IYsyMealOrderService {

    @Autowired
    private YsyMealMapper ysyMealMapper;

    @Override
    @Transactional
    public Result<String> addMetalOrder(AddMetalOrderQuery addMetalOrderQuery) {
        Result<String> result = new Result<>();
        try {
            YsyMeal ysyMeal = ysyMealMapper.selectOne(Wrappers.<YsyMeal>lambdaQuery()
                    .eq(YsyMeal::getId, addMetalOrderQuery.getMetalId()));
            if (ObjectUtil.isNull(ysyMeal)) {
                result.setCode(500);
                result.setSuccess(false);
                result.setMessage("未找到该套餐");
                return result;
            }

            YsyMealOrder ysyMealOrder = YsyMealOrder.builder()
                    .orderDesc(ysyMeal.getType() == 1 ? "封车服务" + ysyMeal.getName() : "车辆轨迹服务" + ysyMeal.getName())
                    .countNum(ysyMeal.getCountNum())
                    .couponId(null)
                    .couponPrice(BigDecimal.ZERO)
                    .createTime(new Date())
                    .discountDesc(ysyMeal.getDiscountDesc())
                    .discountPrice(ysyMeal.getOriPrice().subtract(ysyMeal.getPrice()))
                    .oriPrice(ysyMeal.getOriPrice())
                    .totalPrice(ysyMeal.getPrice())
                    .userId(addMetalOrderQuery.getUserId())
                    .sourceType(ysyMeal.getType())
                    .build();
            this.baseMapper.insert(ysyMealOrder);
            result.setMessage("订单创建成功");
            result.setCode(200);
            result.setSuccess(true);
            result.setResult(ysyMealOrder.getId());
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("服务器响应失败");
            return result;
        }
    }
}
